package com.ddm.controller.vx;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.ddm.constant.MessageConstant;
import com.ddm.context.BaseContext;
import com.ddm.dto.vxDTO.QuestionDTO;
import com.ddm.entity.vxEntity.Question;
import com.ddm.entity.vxEntity.WrongQuestion;
import com.ddm.exception.DataErrorException;
import com.ddm.result.Result;
import com.ddm.service.vxService.QuetsionsService;
import com.ddm.service.vxService.WrongQuestionService;
import com.ddm.utils.BeanCopyUtil;
import com.ddm.vo.vxVo.BonusPointsVO;
import com.ddm.vo.vxVo.QuestionVO;
import com.ddm.vo.vxVo.WrongQuestionScoreVO;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/user/wrongQuestion")
@Slf4j
public class WrongQuestionController {

    @Autowired
    WrongQuestionService wrongQuestionService;
    @Autowired
    QuetsionsService quetsionsService;

    @GetMapping
    @ApiModelProperty("获取错题列表")
    public Result<List<QuestionVO>> getWrongQuestionList(){
        log.info("获取错题列表");
        LambdaQueryWrapper<WrongQuestion> wrapper = new LambdaQueryWrapper<WrongQuestion>().eq(WrongQuestion::getUserId, BaseContext.getCurrentId());
        List<WrongQuestion> wrongQuestions = wrongQuestionService.list(wrapper);
        List<Long> questionIds = wrongQuestions.stream().map(WrongQuestion::getQuestionId).collect(Collectors.toList());
        List<Question> questions = quetsionsService.listByIds(questionIds);
        List<QuestionVO> questionVOS = BeanCopyUtil.BeanCopyList(questions, QuestionVO.class);
        return Result.success(questionVOS);
    }

    @PostMapping
    @ApiOperation("错题重做")
    public Result<WrongQuestionScoreVO> wrongQuestionRedo(@RequestBody List<QuestionDTO> questionDTOS){
        log.info("错题重做");
        if (questionDTOS ==null){
            throw new DataErrorException(MessageConstant.NULL_DATA);
        }
        return Result.success(wrongQuestionService.wrongQuestionRedo(questionDTOS));

    }


}
